# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os

from conan import ConanFile
from conan.tools.scm import Git
from conan.tools.files import copy, chdir
from conan.tools.layout import basic_layout
from conan.tools.gnu import AutotoolsToolchain, Autotools

required_conan_version = ">=2.13.0"


class ProtobufConan(ConanFile):
    name = "protobuf"
    settings = "os", "compiler", "build_type", "arch"
    exports_sources = ["permissions.ini"]
    generators = "AutotoolsDeps"
    _cmake = None
    options = {
        "shared": [True, False],
        "with_zlib": [True, False],
        "lite": [True, False]
    }
    default_options = {
        "shared": True,
        "with_zlib": False,
        "lite": False
    }
    chost = None

    def layout(self):
        basic_layout(self)

    def requirements(self):
        if self.options.with_zlib:
            self.requires("zlib/[>=1.2.11]")

    def source(self):
        source = self.conan_data["sources"][self.version]
        git = Git(self)
        git.fetch_commit(url=source["url"], commit=source["branch"])

    def generate(self):
        tc = AutotoolsToolchain(self)

        tc.configure_args.append("--with-pic")
        if self.settings.arch == "x86_64":
            tc.configure_args.append("--enable-shared")
            tc.configure_args.append("--disable-static")
        else:
            tc.configure_args.append("--disable-shared")
            tc.configure_args.append("--enable-static")
        if self.options.lite:
            tc.configure_args.append("--enable-lite-runtime")
        
        # 添加编译标志
        tc.configure_args.append("--program-prefix=")
        tc.extra_cflags.append("-w")
        tc.extra_cxxflags.append("-w")
        if self.settings.arch == "armv8":
            buildenv_vars = self.buildenv.vars(self)
            sysroot = buildenv_vars.get('CONAN_CMAKE_SYSROOT')
            tc.extra_cflags.append(f"--sysroot={sysroot}")
            tc.extra_cxxflags.append(f"--sysroot={sysroot}")
        if self.settings.arch == "x86_64":
            tc.configure_args.append("--libdir=${prefix}/lib")
        else:
            tc.configure_args.append("--libdir=${prefix}/usr/lib64")
        tc.generate()

    def build(self):
        with chdir(self, self.source_folder):
            self.run("sed -i 's/,no-obsolete/,no-obsolete >\/dev\/null 2>\&1/' autogen.sh")
            self.run("./autogen.sh")
            env_build = Autotools(self)
            env_build.configure(build_script_folder=self.source_folder)
            env_build.make()
            env_build.install()


    def package(self):
        copy(self, "permissions.ini", src=self.source_folder, dst=self.package_folder)

    def package_info(self):
        libdir = "usr/lib64"
        if self.settings.arch == "x86_64":
            libdir = "lib"
        self.cpp_info.bins = ["protoc"]
        self.cpp_info.bindirs = ["bin"]
        self.cpp_info.libs = ["protobuf"]
        self.cpp_info.libdirs = [libdir]
        self.cpp_info.includedirs = ["include"]

        if self.options.shared:
            self.runenv_info.append_path("LD_LIBRARY_PATH", os.path.join(self.package_folder, libdir))
            self.buildenv_info.append_path("LD_LIBRARY_PATH", os.path.join(self.package_folder, libdir))
            self.runenv_info.append_path("PATH", os.path.join(self.package_folder, "bin"))
            self.buildenv_info.append_path("PATH", os.path.join(self.package_folder, "bin"))
        
        self.buildenv_info.define("PROTOBUF_ROOT", self.package_folder)

